package org.twndmv.it;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.List;

import javax.naming.InitialContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Before;
import org.junit.Test;
import org.twndmv.bo.Sex;
import org.twndmv.dto.PersonDTO;
import org.twndmv.dto.PhysicalDetailsDTO;
import org.twndmv.ejb.DmvTestUtilRemote;
import org.twndmv.ejb.PersonMgmtRemote;

public class PersonMgmtIT {
	private static Log log = LogFactory.getLog(PersonMgmtIT.class);
	private InitialContext jndi;

	private static final String dmvPersonJNDI = System.getProperty("jndi.name.person",
            "twnDmvEAR/twnDmvEJB/PersonMgmtEJB!org.twndmv.ejb.PersonMgmtRemote");
	private static final String dmvTestUtilJNDI = System.getProperty("jndi.name.dmvTestUtil",
            "twnDmvEAR/twnDmvEJB/DmvTestUtilEJB!org.twndmv.ejb.DmvTestUtilRemote");
	
	private DmvTestUtilRemote testUtil;

    private PersonMgmtRemote personManager;

    @Before
	public void setUp() throws Exception {
		log.debug("===== TestDmvIT.init =====");
	        jndi = new InitialContext();
	        jndi.lookup("/"); //do a quick comms check of JNDI

	        testUtil = (DmvTestUtilRemote)jndi.lookup(dmvTestUtilJNDI);
	        personManager = (PersonMgmtRemote)jndi.lookup(dmvPersonJNDI);
	        
	        assertNotNull(testUtil);
	        assertNotNull(personManager);
	        testUtil.resetAll();
	}
    
    @Test
    public void testPersonManager() {
    	
    	PersonDTO testPerson = new PersonDTO();
	    testPerson.setFirstName("Steve");
	    testPerson.setMiddleName("Danger");
	    testPerson.setLastName("Jobs");
	    PhysicalDetailsDTO testDetails = new PhysicalDetailsDTO();
	    testDetails.setHeight(64);
	    testDetails.setWeight(180);
	    testDetails.setSEX(Sex.MALE);
	    testPerson.setPhysicalDetails(testDetails);
	    personManager.createPerson(testPerson);
    	
	    List<PersonDTO> resultPeeps = personManager.getPeopleByLastName("Jobs");
    	assertNotNull(resultPeeps);
    	assertEquals(1, resultPeeps.size());
    	PersonDTO result = resultPeeps.get(0);
    	assertNotNull(result);
    	assertFalse(result.getMiddleName().equalsIgnoreCase("Caution"));
    	result.setMiddleName("Caution");
    	personManager.updatePerson(result);
    	PersonDTO peep = personManager.getPersonByID(result.getId());
    	assertTrue(result.getFirstName().equalsIgnoreCase(peep.getFirstName()));
    	//assertTrue(result.getMiddleName().equalsIgnoreCase(peep.getMiddleName()));
    	assertTrue(result.getLastName().equalsIgnoreCase(peep.getLastName()));
    	
	    
    }
}
